package com.example.polarity;

import java.util.Vector;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.ViewGroup;
import android.widget.ProgressBar;

public class GameView extends SurfaceView implements SurfaceHolder.Callback 
{
	public Player player;
	
	private MainActivity mainActivity;
	private MainThread thread;
	public Canvas mainCanvas;
	
	//public MagnetGUI curMagnet = null;
	public Magnet curMagnet; 
	public Vector<Magnet> MagnetList = new Vector<Magnet>();
	public Vector<Bullet> BulletList = new Vector<Bullet>();
	
	public Activity activity; 
	 
	ViewGroup GameObjects; 
	public Canvas GameViewCanvas; 
	public Magnet magnetFocused; 
	public boolean magnetTouched = false;
	
	public float mouseX; 
	public float mouseY;
	
	long touchedTime;
	
	int ScreenWidth;
	int ScreenHeight;
	

	
	//lets make this a surfaceview that runs the thread
	public GameView(Context context) {
		super(context);
		this.player  = new Player(BitmapFactory.decodeResource(getResources(), R.drawable.ship1),"ship1");
		// TODO Auto-generated constructor stub
	}
	
	public GameView(Context context, AttributeSet attrs) {
		
		super(context, attrs);
		//MainActivity v = (MainActivity)context;
		
		this.setWillNotDraw(false);
		this.player  = new Player(BitmapFactory.decodeResource(getResources(), R.drawable.ship1),"ship1");
		 //this.setBackgroundColor(Color.TRANSPARENT);

		if(!isInEditMode())
			mainActivity = (MainActivity)context;
		
		 getHolder().addCallback(this);
		 thread = new MainThread(getHolder(), this);
		 
		 setFocusable(true);
		 
		 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
		 ScreenWidth  = metrics.widthPixels;
		 ScreenHeight = metrics.heightPixels;
		 
		 // mainActivity.BoostBar.setRotation(180);
		 //mainActivity.BoostBar.setMax(100);
		 //BoostBar = (ProgressBar) mainActivity.findViewById(R.id.BoostBar); 
		 // TODO Auto-generated constructor stub
	}
	
	public GameView(Context context, AttributeSet attrs, int defStyle) 
	{
		super(context, attrs, defStyle);
		this.setWillNotDraw(false);
		this.player  = new Player(BitmapFactory.decodeResource(getResources(), R.drawable.ship1),"ship1");
		 
		//GameObjects = (ViewGroup)this.findViewById(R.layout.level1);

		setFocusable(true);
	 }


	 @Override
	 protected void onDraw(Canvas canvas) 
	 {
		 //this.setBackgroundColor(Color.TRANSPARENT);
		 mainCanvas = canvas;
		
		 invalidate();
		 
		 if(this.curMagnet != null)
		 {
			 this.curMagnet.Draw(canvas);
		 }
		 
		 if(MagnetList.size() > 0)
		 {
			 for(int i = 0; i < MagnetList.size(); i++)
			 {
				 Magnet m = MagnetList.get(i);
				 m.Draw(canvas);
			 }
		 }
		 
		 if(mainActivity != null && mainActivity.StartPlayButton.getText().equals("Reset"))
		 {
			 
			 
			 if(this.mainActivity.FireWeapon)
			 {
				 Bullet b = new Bullet( (this.player.x + this.player.PlayerImage.getWidth()), this.player.y , BitmapFactory.decodeResource(getResources(), R.drawable.bullet), mainActivity.joystick.angle);
				 this.BulletList.add(b);
				 this.mainActivity.FireWeapon = false;
			 }
			 
			 for(int i = 0; i <  this.BulletList.size(); i++)
			 {
				 
				 Bullet b = this.BulletList.get(i);
				 b.draw(canvas,this.player,ScreenWidth,ScreenHeight);
				 
				 
				if(b.xPos > ScreenWidth || b.xPos < 0 || b.yPos < 0 || b.yPos > ScreenHeight)
					this.BulletList.remove(this.BulletList.get(i));
				 //if(this.BulletList.get(i).destroy == true);
				 	
			 }
			 
			 this.player.Move(canvas, this,mainActivity.joystick, mainActivity.UseBoost,mainActivity.BoostBar);

		 }
		 else
		 {
			 this.player.playerMagVX = 0;
			 this.player.playerMagVY = 0;
			 this.player.DrawPlayer(canvas);
			 this.player.BoostProgress = 100;
			 
			 if(mainActivity != null)
				 mainActivity.BoostBar.setProgress(100);
		 }
	 }


   @Override
   public boolean onTouchEvent(MotionEvent e)
   {
	   
	   if(mainActivity.StartPlayButton.getText().equals("Start"))
	   {
		 if(MagnetList.size() > 0)
		 {
			 //check if the mouse was down and if it was then move the magnet
			 for(int i = 0; i < MagnetList.size(); i++)
			 {
				 Magnet m = MagnetList.get(i);
				 
				 if(e.getAction() == MotionEvent.ACTION_DOWN && this.magnetTouched == false && m.UpdatePosition(e))
				 {
					 this.magnetFocused = m;
					 this.magnetTouched = true;
					 
					 this.mouseX = e.getX();
					 this.mouseY = e.getY();
					 
					 touchedTime = System.currentTimeMillis();
					 m.bringToFront();
					 break;
				 }
				 
			 }
			 
			 if(this.magnetTouched && e.getAction() == MotionEvent.ACTION_MOVE)
				 this.magnetFocused.setPosition(e);
			 
			 if(e.getAction() == MotionEvent.ACTION_UP)
			 {
				 this.magnetTouched = false;
				 
				 if(this.mouseX == e.getX() && this.mouseY == e.getY() && touchedTime > 20000 )
				 {
					 MagnetList.remove(magnetFocused);
					 touchedTime = 0;
				 }
				 //IF THE MAGNET DID NOT MOVE AND ITS PASSED 20 SECONDS DELETE
			 }
		 }
	   }
	   return true;
   }
	 
	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		mainActivity.BoostBar.setProgress(100);
	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		thread.setRunning(true);
		thread.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		
	} 
}
